package com.vgulu.server.common.utils;

/**
 * Created by wangchunsee on 15/6/9.
 */
public class ShareCodeUtil {
    private static final String CHAR_STORE = "QWERTYUIPASDFGHJKLZXCVBNM123456789";
    private static int ADD_ONE = 1679616;

    public static String shareCodeBySeed(int seed){
        int len=CHAR_STORE.length();
        StringBuilder code=new StringBuilder();
        seed=seed+ADD_ONE;
        while (true){
            int pos=seed%len;
            seed=seed/len;
            code.append(CHAR_STORE.charAt(pos));
            if (seed==0){
                break;
            }
        }
        return code.toString();
    }

    public static int seedByShareCode(String code){
        if (code == null){
            return -1;
        }
        int len = CHAR_STORE.length();
        int codeLen = code.length();
        int seed = 0;
        for (int i=0;i<codeLen;i++){
            int pos = CHAR_STORE.indexOf(code.charAt(codeLen-i-1));
            if (pos < 0){
                return 0;
            }
            seed=seed*len+pos;
        }
        seed-=ADD_ONE;
        if (seed<0){
            return 0;
        }
        return seed;
    }

    public static void main(String[] arg){
        String code=shareCodeBySeed(103041);
        System.out.println("****************"+code+"****************");
        long seed=seedByShareCode(code);
        System.out.println("****************"+seed+"****************");


    }
}
